package com.example.lj.uav.view;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.sqlite.SQLiteDbHelper;
import com.example.lj.uav.view.base.BaseActivity;
import com.example.lj.uav.widget.FlightRecordDialog;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class FlightRecordersActivity extends BaseActivity implements View.OnClickListener {

    private View[] titlt_v;
    private CheckBox[] title_cb;
    private CheckBox cb_equipment;
    private RecyclerView rv_record;
    private List<Map<String, Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flightrecorders);
        ((TextView) findViewById(R.id.flightrecord_tv_acountname)).setText(MyApplication.accountName);
        findViewById(R.id.flightrecord_fl_back).setOnClickListener(this);
        findViewById(R.id.flightrecord_fl_refresh).setOnClickListener(this);
        findViewById(R.id.flightrecord_fl_equipment).setOnClickListener(this);
        cb_equipment = findViewById(R.id.flightrecord_cb_equipment);
        cb_equipment.setOnClickListener(this);

        initTitleView();
        initDate();
    }

    private void initTitleView() {
        findViewById(R.id.flightrecord_ll_title_0).setOnClickListener(this);
        findViewById(R.id.flightrecord_ll_title_1).setOnClickListener(this);
        findViewById(R.id.flightrecord_ll_title_2).setOnClickListener(this);
        findViewById(R.id.flightrecord_ll_title_3).setOnClickListener(this);

        titlt_v = new View[4];
        titlt_v[0] = findViewById(R.id.flightrecord_v_title_0);
        titlt_v[1] = findViewById(R.id.flightrecord_v_title_1);
        titlt_v[2] = findViewById(R.id.flightrecord_v_title_2);
        titlt_v[3] = findViewById(R.id.flightrecord_v_title_3);

        title_cb = new CheckBox[4];
        title_cb[0] = findViewById(R.id.flightrecord_cb_title_0);
        title_cb[1] = findViewById(R.id.flightrecord_cb_title_1);
        title_cb[2] = findViewById(R.id.flightrecord_cb_title_2);
        title_cb[3] = findViewById(R.id.flightrecord_cb_title_3);
    }

    private void chooseTitleSort(int n) {
        for (int i = 0; i < 4; i++) {
            if (i == n) {
                if (title_cb[i].getVisibility() == View.GONE) {
                    title_cb[i].setVisibility(View.VISIBLE);
                    titlt_v[i].setVisibility(View.GONE);
                }
                title_cb[n].setChecked(!title_cb[n].isChecked());
            } else {
                if (title_cb[i].getVisibility() == View.VISIBLE) {
                    title_cb[i].setVisibility(View.GONE);
                    titlt_v[i].setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private void sort(final int n) {
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                int res = 0;
                switch (n) {
                    case 0:
                        String m1 = map1.get("date").toString().replaceAll(" ", "")
                                .replaceAll("-", "")
                                .replaceAll(":", "");
                        String m2 = map2.get("date").toString().replaceAll(" ", "")
                                .replaceAll("-", "")
                                .replaceAll(":", "");
                        res = (Integer.valueOf(m1)).compareTo(Integer.valueOf(m2));
                        break;
                    case 1:
                        res = ((Float) map1.get("distance")).compareTo((Float) map2.get("distance"));
                        break;
                    case 2:
                        res = ((Integer) map1.get("time")).compareTo((Integer) map2.get("time"));
                        break;
                    case 3:
                        res = ((Float) map1.get("height")).compareTo((Float) map2.get("height"));
                        break;
                }
                return res;
            }
        });

        rv_record.setAdapter(null);
        rv_record.setAdapter(new RecordAdapter(list));
    }

    private void initDate() {
        SQLiteDbHelper sqLiteDbHelper = new SQLiteDbHelper(this);
        list = sqLiteDbHelper.dbQuery();
        if (list == null || list.size() == 0)
            return;

        ((ProgressBar) findViewById(R.id.flightrecord_pb_lv)).setProgress(list.size() % 10 * 10);
        ((TextView) findViewById(R.id.flightrecord_tv_lv)).setText("LV" + list.size() / 10);
        findViewById(R.id.flightrecord_rl_norecord).setVisibility(View.GONE);
        rv_record = findViewById(R.id.flightrecord_rv_record);
        rv_record.setVisibility(View.VISIBLE);
        rv_record.setLayoutManager(new LinearLayoutManager(this));
        rv_record.setAdapter(new RecordAdapter(list));

        int totalTime = 0;
        float totalDistance = 0;
        float maxDistance = 0;
        float maxSpeed = 0;
        float maxTakeoff = 0;

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            totalTime += (int) map.get("time");
            totalDistance += (float) map.get("distance");
            if ((float) map.get("distance") > maxDistance) {
                maxDistance = (float) map.get("distance");
            }
            if ((float) map.get("speed") > maxSpeed) {
                maxSpeed = (float) map.get("speed");
            }
            if ((float) map.get("takeoff") > maxTakeoff) {
                maxTakeoff = (float) map.get("takeoff");
            }
        }

        ((TextView) findViewById(R.id.flightrecord_tv_totaltime)).setText(totalTime / 60 + "min" + totalTime % 60 + "s");
        ((TextView) findViewById(R.id.flightrecord_tv_totaldistance)).setText((int) totalDistance + "m");
        ((TextView) findViewById(R.id.flightrecord_tv_times)).setText(list.size());
        ((TextView) findViewById(R.id.flightrecord_tv_maxdistance)).setText(maxDistance + "m");
        ((TextView) findViewById(R.id.flightrecord_tv_maxspeed)).setText(maxSpeed + "m/s");
        ((TextView) findViewById(R.id.flightrecord_tv_maxtakeoff)).setText(maxTakeoff + "m");


    }

    private class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.RecordHolder> {

        private List<Map<String, Object>> list;

        public RecordAdapter() {
            list = new ArrayList<>();
        }

        public RecordAdapter(List<Map<String, Object>> list) {
            this.list = list;
        }

        @Override
        public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new RecordHolder(LayoutInflater.from(FlightRecordersActivity.this).inflate(R.layout.item_record, parent,
                    false));
        }

        @Override
        public void onBindViewHolder(RecordHolder holder, int position) {
            holder.tv_date.setText(list.get(position).get("date").toString());
            holder.tv_distance.setText(list.get(position).get("distance").toString());
            holder.tv_time.setText(list.get(position).get("time").toString());
            holder.tv_height.setText(list.get(position).get("height").toString());
        }

        @Override
        public int getItemCount() {
            return 0;
        }

        class RecordHolder extends RecyclerView.ViewHolder {

            TextView tv_date, tv_distance, tv_time, tv_height;

            public RecordHolder(View itemView) {
                super(itemView);
                tv_date = findViewById(R.id.record_tv_date);
                tv_distance = findViewById(R.id.record_tv_distance);
                tv_time = findViewById(R.id.record_tv_time);
                tv_height = findViewById(R.id.record_tv_height);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.flightrecord_fl_back:
                finish();
                break;

            case R.id.flightrecord_fl_refresh:
                showFlightRecordDialog();
                break;

            case R.id.flightrecord_fl_equipment:
                cb_equipment.setChecked(!cb_equipment.isChecked());
            case R.id.flightrecord_cb_equipment:
                showEquipmentDialog();
                break;

            case R.id.flightrecord_ll_title_0:
            case R.id.flightrecord_ll_title_1:
            case R.id.flightrecord_ll_title_2:
            case R.id.flightrecord_ll_title_3:
                String entryName = getResources().getResourceEntryName(v.getId());
                chooseTitleSort(Integer.parseInt(entryName.substring(entryName.length() - 1, entryName.length())));
                break;
        }
    }

    private void showFlightRecordDialog() {
        new FlightRecordDialog(FlightRecordersActivity.this, R.style.commondialog,
                new FlightRecordDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, int id) {
                        if (id == 0) {
                            Toast.makeText(FlightRecordersActivity.this, "10", Toast.LENGTH_SHORT).show();
                        } else if (id == 1) {
                            Toast.makeText(FlightRecordersActivity.this, "11", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                    }
                }).show();
    }

    private void showEquipmentDialog() {

    }
}
